import request from '@/utils/request';
import type { WorkflowTemplate } from '@/types';

// 工作流执行接口
export interface ExecuteWorkflowParams {
    params: Record<string, any>;
}

export interface ExecuteWorkflowResult {
    imageUrl: string;
    // 其他结果数据...
}

export const workflowApi = {
    // 获取单个工作流
    getWorkflow: (id: string) =>
        request.get<WorkflowTemplate>(`/api/workflow/${id}`),

    // 获取所有工作流
    getAllWorkflows: () =>
        request.get<WorkflowTemplate[]>('/api/workflow'),

    // 创建工作流
    createWorkflow: (data: Partial<WorkflowTemplate>) =>
        request.post<WorkflowTemplate>('/api/workflow', data),

    // 更新工作流
    updateWorkflow: (id: string, data: Partial<WorkflowTemplate>) =>
        request.put<WorkflowTemplate>(`/api/workflow/${id}`, data),

    // 删除工作流
    deleteWorkflow: (id: string) =>
        request.delete(`/api/workflow/${id}`),

    // 测试工作流
    testWorkflow: (id: string) =>
        request.post(`/api/workflow/${id}/test`),

    // 验证工作流
    validateWorkflow: (data: Partial<WorkflowTemplate>) =>
        request.post('/api/workflow/validate', data),

    // 生成图片
    generateImage: (id: string, params: Record<string, any>) =>
        request.post<{ image: string }>(`/api/workflow/${id}/generate`, params),

    // 点赞工作流
    likeWorkflow: (id: string) =>
        request.post(`/api/workflow/${id}/like`),

    // 导出工作流
    exportWorkflow: (options: {
        workflowId: string;
        includes: string[];
        format: 'json' | 'yaml';
        compress: boolean;
        version: 'current' | 'all' | 'custom';
        customVersion?: string;
        metadata: string[];
    }) =>
        request.post<Blob>(
            `/api/workflow/${options.workflowId}/export`,
            options,
            { responseType: 'blob' }
        ),

    // 导入工作流
    importWorkflow: (file: File) => {
        const formData = new FormData();
        formData.append('file', file);
        return request.post<WorkflowTemplate>('/api/workflow/import', formData, {
            headers: { 'Content-Type': 'multipart/form-data' }
        });
    },

    // 检查工作流名称是否存在
    checkName: (name: string) =>
        request.get<{ exists: boolean }>(`/api/workflow/check-name`, { params: { name } }),

    // 执行工作流
    executeWorkflow(id: string, data: ExecuteWorkflowParams) {
        return request.post<ExecuteWorkflowResult>(`/api/workflow/${id}/execute`, data);
    }
};